package com.bestflashsolutions.components
{
	import com.bestflashsolutions.app.AppUserDataInfo;
	import com.bestflashsolutions.events.UploaderEvent;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import mx.core.UIComponent;
	import mx.utils.UIDUtil;

	public class DJUploader extends UIComponent
	{
		
		private var fileRef:FileReference = new FileReference();
		private var guidFName:String;
		private var allTypes:Array;
		
		public var typeOfSend:int = 0;
		public var dataNote:String = "";
		
		public function destroyed():void{
			fileRef.removeEventListener(Event.SELECT, selectHandler);
			fileRef.removeEventListener(Event.COMPLETE, completeHandler);
			fileRef.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
		public function DJUploader()
		{
			var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
			var textTypes:FileFilter = new FileFilter("Text Files (*.txt, *.rtf, *.pdf, *.doc, *.docx)", "*.txt; *.rtf; *.pdf; *.doc; *.docx");
			allTypes = new Array(imageTypes, textTypes);
			fileRef.addEventListener(Event.SELECT, selectHandler);
			fileRef.addEventListener(Event.COMPLETE, completeHandler);
			fileRef.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		
		private function selectHandler(event:Event):void
		{
			var arrFile:Array = fileRef.name.split(".");
			guidFName = UIDUtil.createUID()+"."+arrFile[1];
			dispatchEvent(new UploaderEvent(UploaderEvent.UPLOAD_SELECT));
		}
		
		public function browseFileRef():void{
			fileRef.browse(allTypes);
		}
		
		private function completeHandler(event:Event):void
		{
			var objc:Object = new Object();
			objc.fname = fileRef.name;
			if(typeOfSend == 0)
				objc.savename = fileRef.name;
			else
				objc.savename = guidFName;
			
			dispatchEvent(new UploaderEvent(UploaderEvent.UPLOAD_COMPLETE,false,false,objc));
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void {
			dispatchEvent(new UploaderEvent(UploaderEvent.UPLOAD_ERROR));
		}
		
		public function sendFunctionUpload():void{
			var pathUrl:String = "http://"+AppUserDataInfo.getInstance().httpRootPath+"/"+AppUserDataInfo.getInstance().phpReqUploadAttachment;		
			var request:URLRequest = new URLRequest(pathUrl);
			var arrFile:Array = fileRef.name.split(".");
			request.method = URLRequestMethod.GET;		
			
			var params:URLVariables = new URLVariables();
			params["uploadername"] = AppUserDataInfo.getInstance().userName;
			params["roomguid"] = AppUserDataInfo.getInstance().userRoomGuid;
			params["fname"] = arrFile[0];
			params["fext"] = arrFile[1];
			params["fcompletename"] = fileRef.name;	
			params["type"] = typeOfSend;
			
			if(typeOfSend == 0)
				params["note"] = dataNote;//txtInputNote.text;
			else
				params["desiredfilename"] = guidFName;
				
			request.data = params;				
			
			try
			{
				fileRef.upload(request);
			}
			catch (error:Error)
			{
				trace("Unable to upload file.");
			}
		}
		
	}
}